import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
//注意本类 实现了ActionListener接口 ,需要重写actionPerformed方法
public class DemoButton extends JFrame implements ActionListener{
	JLabel jl;
	public static final String ButtonRed= "红色";
	public static final String ButtonGreen= "绿色";
	public static final String ButtonBlue= "蓝色";
	public DemoButton() {
		jl = new JLabel("hello java!",JLabel.CENTER);//文字,并设置居中
		add(jl);
		JButton jb1 = new JButton(ButtonRed);
		JButton jb2 = new JButton(ButtonGreen);
		JButton jb3 = new JButton(ButtonBlue);
		jb1.setActionCommand(ButtonRed);
		jb2.setActionCommand(ButtonGreen);
		jb3.setActionCommand(ButtonBlue);
		//按钮命令参数
		jb1.addActionListener(this);
		jb2.addActionListener(this);
		jb3.addActionListener(this);
		//设置按钮被点击后的事件响应,去执行actionPerformed力的方法
		JPanel jp = new JPanel();
		jp.add(jb1);
		jp.add(jb2);
		jp.add(jb3);
		add(jp,BorderLayout.SOUTH);
		setSize(300, 130);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			String cmd = e.getActionCommand();
			if(cmd.equals(ButtonRed)){
				jl.setForeground(Color.RED);
			} else if(cmd.equals(ButtonGreen)) {
				jl.setForeground(Color.GREEN);
			} else if(cmd.equals(ButtonBlue)) {
				jl.setForeground(Color.BLUE);
			}
		}		

		public static void main(String[] args) {
			new DemoButton();
		}
		
}